<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">

        <el-form-item class="input-prepend restyle" prop="id" :rules="[{ required: true, message: '请输入账号', trigger: 'blur' }]">
          <div >
            <el-input type="text" placeholder="账号ID" v-model="user.id"/>
            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password"/>
            <i class="iconfont icon-password"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="submitLogin()">
        </div>
      </el-form>
    </div>

  </div>
</template>

<script>
  import '~/assets/css/sign.css'
  import '~/assets/css/iconfont.css'
  import cookie from 'js-cookie'

  import loginApi from '@/api/login'
  export default {
    layout: 'sign',
    data () {
      return {
       user:{
          id:'',
          password:''
        },
        redirectURL:'/',
        loginInfo:{}
      }
    },
    mounted() {
        let rediretUrl = this.$route.query.ref;
        if (rediretUrl){
          this.redirectURL = rediretUrl
        }
    },
    methods: {
      submitLogin(){
            loginApi.submitLogin(this.user).then(response => {
              if(response.data.success){

               cookie.set('en_token', response.data.data.token, { domain: 'localhost' })
               loginApi.getLoginInfo().then(response => {

                  this.loginInfo = response.data.data.userInfo
                 cookie.set('en_user', this.loginInfo, { domain: 'localhost' })
                 window.location.href = "/";
                })
              }
            })
      },
    }
  }
</script>
<style>
   .el-form-item__error{
    z-index: 9999999;
  }
</style>
